/* 
 * File:   QualityMeasurer.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/QualityMeasurer.h"

QualityMeasurer::QualityMeasurer(string pathUser, string pathHeuristic) {
    setVectorOfUserCells(pathUser);
    setVectorOfHeuristicCells(pathHeuristic);
}

QualityMeasurer::QualityMeasurer(const QualityMeasurer& orig) {
}

QualityMeasurer::~QualityMeasurer() {
}

/**
 * Sets the vector of user cells.
 * 
 * @param pathUser
 */
void QualityMeasurer::setVectorOfUserCells(string pathUser) {
    Reader* reader = new Reader(pathUser);
    vector<CvPoint> points = reader->readCells();
    vector<Cell> cells;
    
    vector<CvPoint>::iterator itPoints;
    for(itPoints = points.begin(); itPoints != points.end(); itPoints++) {
        CvPoint currentPoint = *itPoints;
        CvPoint nextPoint = *(++itPoints);
        
        cells.push_back(Cell(currentPoint, nextPoint));
    }
    
    this->cellsUser = cells;
}

/**
 * Sets the vector of heuristic cells.
 * 
 * @param pathHeuristic
 */
void QualityMeasurer::setVectorOfHeuristicCells(string pathHeuristic) {
    Reader* reader = new Reader(pathHeuristic);
    vector<CvPoint> points = reader->read();
    vector<Cell> cells;
    
    vector<CvPoint>::iterator itPoints;
    for(itPoints = points.begin(); itPoints != points.end(); itPoints++) {
        CvPoint currentPoint = *itPoints;
        CvPoint nextPoint = *(++itPoints);
        
        cells.push_back(Cell(currentPoint, nextPoint));
    }
    
    this->cellsHeuristic = cells;
}

/**
 * Analise the level of acceptance
 * of the cells indicated by the
 * heuristic of table transcriber.
 * Returns the percents of false positives
 * and true positives in the total of heuristic answers.
 * 
 * @return percents
 * 
 * vector[0] == falses positives percent
 * vector[1] == true positives percent
 */
vector<double> QualityMeasurer::analiseQualityOfTranscrition()  {
    vector<double> ans;
    
    vector<Cell>::iterator itUserAns;
    vector<Cell>::iterator itHeuristicAns;
    
    double percentTruePositives = 0.0;
    double percentFalsePositives = 0.0;
    
    for(itUserAns = this->cellsUser.begin(); itUserAns != this->cellsUser.end(); itUserAns++) {
        int cellHeigth = (*itUserAns).rightBottom.y - (*itUserAns).leftUpper.y;
        int cellWidth = (*itUserAns).rightBottom.x - (*itUserAns).leftUpper.x;

        CvPoint newLU, newRB;
        newLU.x = (*itUserAns).leftUpper.x + 0.1 * cellWidth;
        newLU.y = (*itUserAns).leftUpper.y + 0.1 * cellHeigth;
        newRB.x = (*itUserAns).rightBottom.x + 0.1 * cellWidth;
        newRB.y = (*itUserAns).rightBottom.x + 0.1 * cellWidth;

        Cell* newCellExpanded = new Cell(newLU, newRB);
        
        for(itHeuristicAns = this->cellsHeuristic.begin(); itHeuristicAns != this->cellsHeuristic.end(); itHeuristicAns++) {
            if(!(*newCellExpanded).contains(*itHeuristicAns)) {
                percentFalsePositives++;
            }
            else {
                percentTruePositives++;
            }
        }
    }
    
    percentFalsePositives /= this->cellsHeuristic.size();
    percentTruePositives /= this->cellsHeuristic.size();
    
    ans.push_back(percentFalsePositives);
    ans.push_back(percentTruePositives);
    
    return ans;
}

